import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

public class ServletDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //如果没有设置的话，tomcat默认编码为 iso 8859-1
        //POST可以设置请求头  改变编码

            StringBuffer st = new StringBuffer();

            BufferedReader reader =  request.getReader();
            String line = null;
            while ((line = reader.readLine())!=null){
                st.append(line);
            }
        System.out.println(st.toString());





//        User user = new User();
//        user.setName("老王");
//        user.setAge(28);
//        ObjectMapper mapper = new ObjectMapper();
//        String userJson = mapper.writeValueAsString(user);
//        response.setContentType("application/json;charset=UTF-8");
//        response.getWriter().write(userJson);

//        String name =request.getParameter("nameP");//从tomcat通过.getBytes拿到 ISO-8859-1的二进制流，转换为UTF-8
//        request.setAttribute("name",name);
//        request.getRequestDispatcher("/WEB-INF/demo.jsp").forward(request,response);
//        String pwd = request.getParameter("pwdP");
//        String xx = (String) request.getSession().getAttribute("name");
//        System.out.println(xx);
//        //servlet 三大域
//        // request  请求域 声明周期很短     ServletRequest
//        // session  会话  默认30分钟左右  控制登录  标记登录状态  标识一个会话  HttpSession
//        // context  生命周期是最长的  应用域  伴随着整个应用  ServletContext
//        // 监听器就是监听以上三个域
//        Cookie[] cookies = request.getCookies();
//        for(int i = 0 ; i<cookies.length; i++){
//            System.out.println(cookies[i].getName());
//            System.out.println(cookies[i].getValue());
//        }
//
//        //session 和 cookies 区别和联系
//        //session 是基于 cookie的 客户端首次访问服务器时会在客户端生成一个JsessionId，下次访问时浏览器会带上sessionid
//        Cookie cookie = new Cookie("name",name);
//        cookie.setMaxAge(60*60*24);
//        response.addCookie(cookie);
//
////        String p = request.getContextPath();
////        String x = request.getRequestURI();
////        String a = request.getRequestURL().toString();
////        String y = request.getRemoteAddr();
////        String z = request.getRemoteHost();
////        Map<String,String[]> map = request.getParameterMap();
//
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out =  response.getWriter();//输出一个字符流的方法
//
////        try{
////            out.print("<html>");
////            out.print("<head>");
////            out.print("<title>Demo1Servlet</title>");
////            out.print("</head>");
////            out.print("<body>");
////            out.print("<h2>Hello "+ name + ", 欢迎学习servlet" + "</h2>");
////            out.print("<h2>The time right now is : " + new Date() + "</h2>");
////            out.print("</body>");
////            out.print("</html>");
////        }finally{
////            out.close();
////        }
//
//        System.out.println(name+"---------------"+pwd);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //get请求  是没有办法改变编码的  可以先修改tomcat server.xml中的配置
        String name =new String (request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
        String pwd = request.getParameter("pwd");

        System.out.println(name+"---------------"+pwd);
    }
}
